package auxiliary;

import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.DefaultListModel;
import javax.swing.JList;

public class IndexedList extends JList {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int index;
	ArrayList<String> values;
	
	public IndexedList(DefaultListModel defaultListModel, String[] names, ArrayList<String> values, int index) {
		super(defaultListModel);
		this.values = new ArrayList<String>(values);
		ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(names));
		this.updateList(wordList,values);
		
		this.index = index;
	}
	
	
	public IndexedList(DefaultListModel defaultListModel, int index) {
		super(defaultListModel);
		
		values = new ArrayList<String>();
		
		this.index = index;
	}

	public int getIndex() {
		return index;
	}
	
	public  ArrayList<String> getValues() {
		int[] indices = this.getSelectedIndices();
		ArrayList<String> result = new ArrayList<String>();
		
		for(int i =0; i < indices.length; i++) {
			result.add(values.get(indices[i]));
		}
		
		return result;
	}
	
	public  String getValue() {
		return values.get(this.getSelectedIndex());
	}
	
	public void updateList(ArrayList objs,ArrayList<String> values)//your standard list update method
    {
       DefaultListModel listModel = (DefaultListModel) this.getModel();
        listModel.clear();
        this.values = new ArrayList<String>();
        for(int i=0; i<objs.size(); i++)
        {
        	this.values.add(values.get(i));
            listModel.addElement((String)objs.get(i));
        }
    }
	
	
}